package ee.ut.battleship;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletResponse;

// Natuke kavalama variandi leiab siit:
// http://stackoverflow.com/questions/3374703/servlet-filter-for-browser-caching

/**
 * Servlet Filter implementation class CachingFilter
 */
@WebFilter({ "*.css", "*.js", "*.png" })
public class CachingFilter implements Filter {

    /**
     * Default constructor. 
     */
    public CachingFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// place your code here

		HttpServletResponse resp = (HttpServletResponse)response;

		Calendar inTwoMonths = new GregorianCalendar();
		inTwoMonths.add(Calendar.MONTH, 2);
		resp.setDateHeader("Expires", inTwoMonths.getTimeInMillis());
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
